function Channel(url, channelId, options) {
	//Options used by this channel
	this.options = options ? options : {};
	$.extend(this.options, Channel.default_options);

	//Channel URL
	this.url = url;
	this.channelId = channelId;
	this.retry = this.options.retry;
}

Channel.default_options = {
		timeout : 120000,
		retry_time : 3000,
		retry: 5
}

Channel.prototype = {
		
		pool : function() {
			var $this = this;
			
			if($this.shouldWait) {
				$this.shouldWait = false;
				
				if($this.retry <= 0) {
					//It is a persistent error
					console.log("Persistent error, channel closed.");
					return;
				}
				
				console.log("Retry in " + ($this.options.retry_time/1000) + " sec.");
				setTimeout(function(){
					console.log("Retry:");
					$this.pool();
					},
					$this.options.retry_time);
			}else {
				
				console.log("Pooling...");
				$.ajax({ url: $this.url, data: {channel: $this.channelId}, success: $this.success, dataType: "json", 
					complete: $this.pool, error: $this.error, timeout: $this.options.timeout,
					context: $this}); 
				//context is pretty important attribute, it tells which will be 
				//the context of callback functions.
			}
		},

		success : function(data) {
			$this = this;
			
			//Retore the retry number
			$this.retry = $this.options.retry;
			
			console.log("Received data from server: %o ", data);
			
			if($this.options.onServerMessage) {
				for(msg in data) {
					$this.options.onServerMessage.call($this, data[msg].msg);
				}
			}
		},
		
		error : function(jXHR, error, exception) {
			
			$this = this;
			
			//Decrease number of retrys, when it reach 0 stop the pooling
			if(error != "timeout") {
				$this.retry--;
				$this.shouldWait = true;
			}
			
		}
}